/**
 * 
 */
package de.dnb.basics.statistics;

import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import de.dnb.basics.applicationComponents.Pair;
import de.dnb.basics.applicationComponents.StringUtils;
import de.dnb.basics.filtering.IFunction;

/**
 * Implementation of Multimap that uses an ArrayList to store the values for a 
 * given key. A TreeMap associates each key with an TreeSet of values.
 * 
 * This multimap allows duplicate key-value pairs. After adding a new 
 * key-value pair equal to an existing key-value pair, the ArrayListMultimap 
 * will contain entries for both the new value and the old value. 
 * Keys and values may be null. 
 * 
 * @author Christian_2
 *
 * @param <K>
 * @param <V>
 */
public class TreeMultimap<K extends Comparable<K>, V extends Comparable<V>>
	extends Multimap<K, V> implements Serializable {

	@Override
	protected Collection<V> getNewValueCollection() {
		return new TreeSet<>(valueComparator);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 5189502140941869729L;

	private final Comparator<V> valueComparator;

	public TreeMultimap() {
		super(new TreeMap<K, Collection<V>>());
		this.valueComparator = null;
	}

	public TreeMultimap(
		Comparator<K> keyComparator,
		Comparator<V> valueComparator) {
		super(new TreeMap<K, Collection<V>>(keyComparator));
		this.valueComparator = valueComparator;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Multimap<String, String> multimap =
			new TreeMultimap<>(String.CASE_INSENSITIVE_ORDER,
				String.CASE_INSENSITIVE_ORDER);
		multimap.add("D");
		multimap.add("A", "x");
		multimap.add("A", "Y");
		multimap.add("A", "z");
		multimap.add("A", "X");

		multimap.add("b", "1");
		multimap.add("b", "2");

		multimap.add("C", "1");

		multimap.add("A");
		
		

		System.out.println(multimap);
		System.out.println();
		
		for (Iterator<String> iterator = multimap.valuesIterator(); iterator
			.hasNext();) {
			System.out.println(iterator.next());			
		}
	}

}
